﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Adapter.SISCONVE.Helper;
using Adapter.SISCONVE.Pages;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.TestTemplates;

namespace Adapter.SISCONVE.Components
{
    public class Suite : BaseTest
    {

        #region Objects

        private const BrowserType BROWSER_TYPE = BrowserType.Chrome;
        private LoginPage _loginPage;

        #endregion

        #region Constructor

        public Suite()
            : base()
        {
            //Initialize the manager object
            Initialize();

            //Initialize objects of other classes
            _loginPage = new LoginPage(Manager);
            
            //Open the browser and login 
            Manager.LaunchNewBrowser(BROWSER_TYPE);
            _loginPage.Login(ConfigurationManager.AppSettings["user"], ConfigurationManager.AppSettings["password"]);
        }
        #endregion

        protected DataTable ReadExcelFile(string file)
        {
            DataSet ds = new DataSet();

            string connectionString = Utilities.GetConnectionString(file);

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;

                // Get all Sheets in Excel File
                DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                // Loop through all Sheets to get data
                foreach (DataRow dr in dtSheet.Rows)
                {
                    string sheetName = dr[2].ToString();

                    if (!sheetName.EndsWith("$"))
                        continue;

                    // Get all rows from the Sheet
                    cmd.CommandText = "SELECT * FROM [" + sheetName + "]";

                    DataTable dt = new DataTable();
                    dt.TableName = sheetName;

                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    da.Fill(dt);

                    ds.Tables.Add(dt);
                }

                cmd = null;
                conn.Close();
            }

            return ds.Tables[0];
        }      
    }
}